﻿using Heima.OA.Common;
using Heima.OA.IBLL;
using Heima.OA.Model;
using Heima.OA.UI.Portal.Controllers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Heima.OA.UI.Portal.Areas.Check.Controllers
{
    public class CheckMemberController : BaseController
    {
        public IZS_CheckMemberService ZS_CheckMemberService { get; set; }
        // GET: Declare/Member
        public string Index(PaginationQuery paginationQuery)
        {
            int total = 0;
            var memberList = ZS_CheckMemberService.GetPageEntities(paginationQuery.pagesize, paginationQuery.pageindex, out total, "  DelFlag=0 ", "");

            PaginationData data = new PaginationData() { total = total, rows = JsonHandler.ToJson(memberList) };
            JsonMessage msg = JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "", JsonHandler.ToJson(data));

            return JsonHandler.ToJson(msg);
        }

        #region GetList
        public string GetList()
        {
            var memberList = ZS_CheckMemberService.GetEntities(u => u.DelFlag == 0 ).ToList();
            JsonMessage msg = JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "", JsonHandler.ToJson(memberList));

            return JsonHandler.ToJson(msg);
        }
        #endregion

        #region 添加
        public string Create(ZS_CheckMember member)
        {
            member.DelFlag = delflagNormal;
            member.CreateTime = DateTime.Now;
            member.DeclareMemberId = 0;
            member.DeclareFamilyId = 0;

            ZS_CheckMemberService.Add(member);
            JsonMessage message = JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "添加成功!");
            return JsonHandler.ToJson(message);
        }

        #endregion
        #region 编辑
        public string Edit(ZS_CheckMember member)
        {
            ZS_CheckMember editMember = ZS_CheckMemberService.GetModel(member.Id);
            string res = string.Empty;
            if (editMember == null)
            {
                res = JsonHandler.ToJson(JsonHandler.CreateMessage((int)MsgTypeEnum.错误, "编辑错误!"));
            }
            else
            {
                editMember.CheckFamilyId = member.CheckFamilyId;
                editMember.MemberName = member.MemberName;
                editMember.WithPowerRelation = member.WithPowerRelation;
                editMember.Sex = member.Sex;
                editMember.MaritalStatus = member.MaritalStatus;
                editMember.Birthday = member.Birthday;
                editMember.IDNumber = member.IDNumber;
                editMember.IdentityType = member.IdentityType;
                editMember.RegisterInfo = member.RegisterInfo;
                editMember.Mobile = member.Mobile;
                editMember.MoveIntoTime = member.MoveIntoTime;

                bool success = ZS_CheckMemberService.Update(editMember);
                if (success)
                {
                    res = JsonHandler.ToJson(JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "编辑成功!"));
                }
            }

            return res;
        }
        #endregion
        #region 删除
        public string Delete(int id)
        {
            ZS_CheckMember member = ZS_CheckMemberService.GetModel(id);
            string res = string.Empty;
            if (member == null)
            {
                res = JsonHandler.ToJson(JsonHandler.CreateMessage((int)MsgTypeEnum.错误, "删除错误!"));
            }
            else
            {
                member.DelFlag = (int)DelFlagEnum.Deleted;
                bool success = ZS_CheckMemberService.Update(member);
                if (success)
                {
                    res = JsonHandler.ToJson(JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "删除成功!"));
                }
            }
            return res;
        }
        #endregion
        #region 成员绑定家庭
        public string SetMemberFamily(int CheckFamilyId, string MemberIds)
        {
            JsonMessage msg = new JsonMessage();
            if(CheckFamilyId < 1 || string.IsNullOrEmpty(MemberIds))
            {
                msg = JsonHandler.CreateMessage((int)MsgTypeEnum.错误, "家庭或成员选择错误!");
            }
            else
            {
                //先将家庭之前绑定的当事人解绑
                var checkMemberList = ZS_CheckMemberService.GetEntities(u => u.DelFlag == 0 && u.CheckFamilyId == CheckFamilyId).ToList();
                foreach(var item in checkMemberList)
                {
                    item.CheckFamilyId = 0;
                    ZS_CheckMemberService.Update(item);
                }
                //家庭重新绑定当事人
                string[] arr = MemberIds.Split(',');
                foreach(var id in arr)
                {
                    var model = ZS_CheckMemberService.GetModel(DataConvert.GetInt(id));
                    if(model != null)
                    {
                        model.CheckFamilyId = CheckFamilyId;
                    }
                    ZS_CheckMemberService.Update(model);
                }
                msg = JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "成员绑定家庭成功!");
            }
            return JsonHandler.ToJson(msg);
        }
        #endregion
        #region 通过当事人姓名或者身份证号码的关键字查询当事人信息
        public string GetMember(string Query)
        {
            var memberList = ZS_CheckMemberService.GetEntities(u => u.DelFlag == delflagNormal && u.MemberName.Contains(Query) ||
            u.IDNumber.Contains(Query));
            JsonMessage msg = JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "", JsonHandler.ToJson(memberList));
            return JsonHandler.ToJson(msg);
        }
        #endregion


    }
}